package dataStructures;
public class ParBracPrintPairs{
    
    public static void printParBracPairs(String str){
        
        int loopcontrol, parCount = 0, bracCount=0;
        ArrayStack stack=new ArrayStack();
        
        
        for(int i=0; i<str.length();i++){
            
            loopcontrol=-1;
            
              if(str.substring(i,i+1).equals("(")){
                     stack.push(new Integer(i));
                     parCount++;
                      
              }    
              else if (str.substring(i,i+1).equals("[")){
                          stack.push(new Integer(i));
                          bracCount++;
                   }
              
          if(!stack.empty()){ // if the stack is empty we do not want to perform and actions
                
        	  //str.substring(((Integer)(stack.peek())).intValue()--this code is used to obtain Integer and cast it to an int.  
        	  if(str.substring(i,i+1).equals(")")&& (str.substring(((Integer)(stack.peek())).intValue(),((Integer)(stack.peek())).intValue()+1).equals("["))){
                  
        		  //we will remove brackets until we find an open parenthesis or if no brackets on stack remove one parenthesis
                  while(loopcontrol<0 && !stack.empty()){
                      
                      if(str.substring(((Integer)(stack.peek())).intValue(),((Integer)(stack.peek())).intValue()+1).equals("[")){
                          
                    	  if(parCount==0){ //for the case we have  "((((]) then remove one parenthesis 
                    		  
                    		  System.out.println(" No matching right parenthesis "+stack.pop());
                    		  
                    		  loopcontrol++;
                    		  
                    		  bracCount--;
                    	  }
                    	  else { System.out.println(" No matching right parenthesis "+stack.pop());
                		       bracCount--;
                    	  }
                      
                      }
                      
                      else {
                          System.out.println(" No matching right parenthesis "+stack.pop());
                          loopcontrol++;
                      }
                  }
                  
              }
              else if (str.substring(i,i+1).equals(")")){
                  
                  try{
                      System.out.println(" "+stack.pop()+" "+i);
                  }
                  
                  catch(Exception e){
                      System.out.println("No matching right parenthesis for the bracket at "+i);
                  }
              }
            

              else  if(str.substring(i,i+1).equals("]")&& (str.substring(((Integer)(stack.peek())).intValue(),((Integer)(stack.peek())).intValue()+1).equals("("))){
                  
                  while(loopcontrol<0 && !stack.empty()){
                      
                      if(str.substring(((Integer)(stack.peek())).intValue(),((Integer)(stack.peek())).intValue()+1).equals("(")){
                    	   
                    	  if(bracCount==0){ //for the case we have  "[[[[)] then remove one bracket
                    		  
                    		  System.out.println(" No matching right parenthesis "+stack.pop());
                    		  
                    		  loopcontrol++;
                    		  
                    		  parCount--;
                    	  }
                    	  else { System.out.println(" No matching right parenthesis "+stack.pop());
                		       parCount--;
                    	  }
                      }
                      
                      else {
                          System.out.println(" No matching right bracket "+stack.pop());
                          loopcontrol++;
                      }
                  }
                  
              }
              else if (str.substring(i,i+1).equals("]")){
                  
                  try{
                      System.out.println(" "+stack.pop()+" "+i);
                  }
                  
                  catch(Exception e){
                      System.out.println("No matching right parenthesis for the bracket at "+i);
                  }
              }
               
        }
    }
        
        while(!stack.empty()){ 
            
                if(str.substring(((Integer)(stack.peek())).intValue(),((Integer)(stack.peek())).intValue()+1).equals("(")){
                  
                        System.out.println(" No matching right parenthesis "+stack.pop());
                }
              
              else   System.out.println(" No matching right bracket "+stack.pop());
        }
        
        
    }
}